package com.wc.service.impl;

import com.wc.dao.UserMapper;
import com.wc.pojo.User;
import com.wc.service.UserService;
import com.wc.util.Page;
import com.wc.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户实现类
 */
@Service
public class UserServiceImpl implements UserService{
	
	@Autowired
	private UserMapper dao;

	protected static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

	//开启事务注解
	@Transactional
	public boolean saveUser(User user) {
		boolean result = false;
		try {
			//只做了简单校验，真实校验需要将用户对象中的参数分别做针对性校验
			if (StringUtils.isEmpty(user) || StringUtils.isEmpty(user.getName()) || StringUtils.isEmpty(user.getIdcard()) || StringUtils.isEmpty(user.getMobiile()) || StringUtils.isEmpty(user.getAddress()) || StringUtils.isEmpty(user.getBirtday()) || StringUtils.isEmpty(user.getAge())){
				throw new Exception("用户信息不能为空");
			}
			if (validateUseridcard(user.getIdcard())){
				throw new Exception("身份证号码不能一致");
			}
			int row = dao.saveUser(user);
			if (row > 0){
				logger.info("添加成功");
				result = true;
			}else {
				logger.error("添加失败");
				result = false;
			}
		}catch (Exception e){
			logger.debug(e.getMessage());
		}
		return result;
	}

	public boolean validateUseridcard(String idcard) {
		boolean result = true;
		try {
			if (StringUtils.isEmpty(idcard)){
				throw new Exception("没有输入身份证号码");
			}
			if (idcard.length() != 18){
				throw new Exception("身份证号码没有18位或超出");
			}
			User user = dao.getUseridcard(idcard);
			//查询到用户表示true 反之false
			if (!StringUtils.isEmpty(user)){
				result = true;
			}else {
				result = false;
			}
		} catch (Exception e) {
			logger.debug(e.getMessage());
		}
		return result;
	}

	public User getUseridcard(String idcard) {
		User user = null;
		try {
			if (StringUtils.isEmpty(idcard)){
				throw new Exception("没有输入身份证号码");
			}
			if (idcard.length() != 18){
				throw new Exception("身份证号码没有18位或超出");
			}
			user = dao.getUseridcard(idcard);
		}catch (Exception e){
			logger.debug(e.getMessage());
		}
		return user;
	}

	@Transactional
	public boolean removeUser(String idcard) {
		/*
			删除用户需要考虑该用户是否存在有价值的东西还存在，如果还存在不能删除，反之可以删除
		 */
		boolean result = false;
		try {
			if (StringUtils.isEmpty(idcard)){
				throw new Exception("请输入需要删除的身份证号码");
			}
			int row = dao.removeUser(idcard);
			if (row > 0){
				logger.info("删除成功");
				result = true;
			}else{
				logger.error("删除失败");
				result = false;
			}
		}catch (Exception e){
			logger.debug(e.getMessage());
		}
		return result;
	}

	@Transactional
	public boolean updateUser(User user) {
		/*
			只修改用户名、地址、手机号
		 */
		boolean result = false;
		try {
			if (StringUtils.isEmpty(user) || StringUtils.isEmpty(user.getName())  || StringUtils.isEmpty(user.getMobiile()) || StringUtils.isEmpty(user.getAddress())){
				throw new Exception("用户信息不能为空");
			}
			int row = dao.updateUser(user);
			if (row > 0){
				logger.info("修改成功");
				result = true;
			}else {
				logger.error("修改失败");
				result = false;
			}
		}catch (Exception e){
			logger.debug(e.getMessage());
		}
		return result;
	}

	public Page<User> getUserlist(int pageNo, int pageSize, String name, String isSort) {
		Page.checkPage(pageNo,pageSize);
		if (StringUtils.isEmpty(isSort)) {
			isSort = "ASC";
		}
		Page<User> page = new Page<User>();
		try {
			page.setPageNo(pageNo);
			page.setPageSize(pageSize);
			int tatalCount = dao.getCount(name);
			page.setTotalCount(tatalCount);
			int totalPage = tatalCount % pageSize == 0 ? tatalCount / pageSize : tatalCount / pageSize + 1;
			page.setTotalPage(totalPage);
			List<User> list = dao.getUserlist((pageNo - 1) * pageSize,pageSize,name,isSort);
			page.setList(list);
		} catch (Exception e) {
			logger.debug(e.getMessage());
		}
		return page;
	}

	public User getUserById(Integer userid) {
		User user = null;
		try {
			if (StringUtils.isEmpty(userid)){
				throw new Exception("没有输入用户id字段");
			}
			user = dao.getUserById(userid);
		}catch (Exception e){
			logger.debug(e.getMessage());
		}
		return user;
	}

}
